//
//  SweetheartApplyViewController.m
//  Molian
//
//  Created by xiaocankeji on 15/8/27.
//  Copyright (c) 2015年 xiaocan. All rights reserved.
//

#import "SweetheartApplyViewController.h"
#import "FriendApplyCell.h"
#import "MLUserDataViewController.h"
//#import "ChatSendHelper.h"
@interface SweetheartApplyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)NSMutableArray *sweetheartApplyList;//恋人申请

@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)Utils * uview;
@property NSInteger sweetCount;//查询的恋人数量
@end

@implementation SweetheartApplyViewController

-(void)cmd:(NSNotification *)notification
{
    NSDictionary *dic=[notification userInfo];
    EMMessage *message = [dic objectForKey:@"message"];
    if ([message.from isEqualToString:@"new_sweet"]) {
        [self loadFriendApplyData];
    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"试恋申请";
    self.view.backgroundColor = APPCOLORBACKGROUNDCOLOR(1);
    [self loadFriendApplyData];
    // 右上角按钮
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc]initWithTitle:@"忽略所有" style:UIBarButtonItemStyleBordered target:self action:@selector(rejectAllApply)];
    [rightBarButton setTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = rightBarButton;
    self.sweetheartApplyList = [NSMutableArray array];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight)style:UITableViewStylePlain];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollsToTop = YES;
    self.tableView.tableHeaderView = [[UIView alloc] init];
    self.tableView.tableFooterView = [[UIView alloc] init];
    self.tableView.separatorColor = RGBA(225, 225, 225, 1);//分隔条颜色
    [self.view addSubview:self.tableView];
    self.uview = [[Utils alloc]init];
    [_uview viewControllerPrompt:self.view.bounds Text:@"没有试恋申请~" Image:[UIImage imageNamed:@"xinlianren_nul"]];
    [self. view addSubview:_uview.viewPrompt];
    if ([self.sweetheartApplyList count] == 0) {
        _uview.viewPrompt.hidden = NO;
    }else{
        _uview.viewPrompt.hidden = YES;
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(cmd:) name:@"cmd" object:nil];
    
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    // [[NSNotificationCenter defaultCenter] removeObserver:self name:@"cmd" object:nil];
    
}
// 加载好友申请列表
- (void)loadFriendApplyData {
    AVQuery *sweetheartApplyQuery = [MLSweetheartApply query];
    [sweetheartApplyQuery whereKey:@"user" equalTo:[MLUser currentUser]];
    [sweetheartApplyQuery includeKey:@"applier"];
    [sweetheartApplyQuery whereKey:@"status" equalTo:[NSNumber numberWithInt:MLSweetheartApplyStatus_WAITING]];
    [sweetheartApplyQuery orderByDescending:@"updatedAt"];
    [sweetheartApplyQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            for (MLSweetheartApply *article in objects) {
                if (article.user) {
                    [self.sweetheartApplyList addObject:article];
                }else{
                    [article deleteInBackground];
                }
            }
            
            if ([self.sweetheartApplyList count] == 0) {
                _uview.viewPrompt.hidden = NO;
            }else{
                _uview.viewPrompt.hidden = YES;
                [self.tableView reloadData];
            }
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

#pragma mark tableview delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString *CellIdentifier = @"FriendApplyCell";
    FriendApplyCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if  (cell == nil) {
        cell = [[FriendApplyCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier];
    }
    // 如果列表为空直接返回
    if ([self.sweetheartApplyList count] == 0) {
        return cell;
    }
    MLSweetheartApply *sweetheartApply = self.sweetheartApplyList[indexPath.row];
    MLUser *applier = (MLUser *)sweetheartApply.applier;
    
    cell.userNameLabel.text = applier.nickname;
   
    if (!applier.subAvatar || [applier.subAvatar isEqualToString:@""]) {
        [cell.userAvatarImg sd_setImageWithURL:[NSURL URLWithString:applier.avatar.url] placeholderImage:[UIImage imageNamed:@"common_avatar_loading_background"]];
        
    }else{
        [cell.userAvatarImg sd_setImageWithURL:[NSURL URLWithString:applier.subAvatar] placeholderImage:[UIImage imageNamed:@"common_avatar_loading_background"]];
    }
    [cell.userAgeLabel setTitle:[NSString stringWithFormat:@"%ld", (long)[applier getUserAge]] forState:UIControlStateNormal];
    if (applier.gender == MLUserGender_MALE) {
        [cell.userAgeLabel setBackgroundImage:[UIImage imageNamed:@"boy_age"] forState:UIControlStateNormal];
    } else {
        [cell.userAgeLabel setBackgroundImage:[UIImage imageNamed:@"girl_age"] forState:UIControlStateNormal];
    }
    
    NSString *city = applier.selectCity?applier.selectCity:@"";
    NSString *strH = applier.height?[NSString stringWithFormat:@"%ldcm",(long)applier.height]:@"";
    NSString *title = [NSString stringWithFormat:@"%@  %@",city,strH];
    [cell.userSeriesLabel setTitle:title forState:UIControlStateNormal];
    cell.userSeriesLabel.selected = YES;
    cell.acceptBtn.tag = indexPath.row;
    [cell.acceptBtn addTarget:self action:@selector(acceptSweetheartApply:) forControlEvents:UIControlEventTouchUpInside];
    
    // 重置remarkLabel高度
    [cell.remarkLabel setFrame:CGRectMake(10, 60, DeviceWidth-20, [Utils LabeSizeLabelheight:sweetheartApply.remark Font:[UIFont fontWithName:FontName size:14]].height +5)];
    
    cell.remarkLabel.text = [NSString stringWithFormat:@"  %@",sweetheartApply.remark];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return  cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    MLSweetheartApply *sweetheartApply = self.sweetheartApplyList[indexPath.row];
    
    MLUserDataViewController *userInfoVC = [[MLUserDataViewController alloc]init];
    userInfoVC.userData = (MLUser *)sweetheartApply.applier;
    [Utils pushTabbarHidden:userInfoVC Selfvc:self];
    
}

//cell个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.sweetheartApplyList count];;
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MLSweetheartApply *sweetheartApply = self.sweetheartApplyList[indexPath.row];
    if (sweetheartApply.remark.length == 0) {
        return 60;
    }else{
        return 70 + [Utils LabeSizeLabelheight:sweetheartApply.remark Font:[UIFont fontWithName:FontName size:14]].height+5;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    // 每个cell的头部间距
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section;
{
    //每个cell的底部间距
    return 0;
}

-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Remove seperator inset
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    
    // Prevent the cell from inheriting the Table View's margin settings
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
    // Explictly set your cell's layout margins
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark user interaction
- (void)addShareDateFriendsUser:(MLUser *)user
{
    //添加为好友
    
    if ([ShareData shareData].friends&&[ShareData shareData].friends.count) {
        //判断是否已经是好友
        BOOL is = NO;
        for (MLUser *f in [ShareData shareData].friends) {
            if([f.objectId isEqualToString:user.objectId]){
                is = YES;
                break;
            }
        }
        
        if (!is) {
            [[ShareData shareData].friends addObject:user];
        }
    }
    [Utils savePassFriendObjectID:user.objectId];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"loadMyFriendsData" object:self userInfo:nil];
    
}

- (void)searchSweetheartCount:(UIButton *)sender
{
    sender.enabled = NO;
    //查询我的恋人数量(正在进行的)
     ShareData *shareData = [ShareData shareData];
    shareData.sweetheartType = sweetheartType_QUERY;
    AVQuery *querySponor = [MLSweetheart query];
    [querySponor whereKey:@"sponsor" equalTo:[MLUser currentUser]];
    [querySponor whereKey:@"status" equalTo:[NSNumber numberWithInteger:1]];
    AVQuery *queryWinner = [MLSweetheart query];
    [queryWinner whereKey:@"winner" equalTo:[MLUser currentUser]];
    [queryWinner whereKey:@"status" equalTo:[NSNumber numberWithInteger:1]];
    AVQuery *query = [AVQuery orQueryWithSubqueries:[NSArray arrayWithObjects:queryWinner,querySponor, nil]];
    [HUD setOnViewWithTitle:@"加载中" animated:YES];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [HUD hideFromViewAnimated:YES];
        if (!error) {
            //没有错误
            shareData.sweetheartType = sweetheartType_DONE;
            sender.enabled = YES;
            if (objects) {
                self.sweetCount = objects.count;
                if(objects.count<3){
                  //  [HUD Prompt:@"可以加为恋人"];
                    [self acceptSweetheartApply:sender];
                }else{
                    [HUD Prompt:@"专心珍惜现在的恋人吧"];
                }
            }
            
            
            
        }else{
            if ([[error localizedDescription] isEqualToString:@"no results matched the query"]) {
                
                 sender.enabled = YES;
                shareData.sweetheartType = sweetheartType_DONE;

                [self acceptSweetheartApply:sender];
               //  [HUD Prompt:@"可以加为恋人"];
                //没有匹配到结果
            }else{
                 sender.enabled = YES;
                shareData.sweetheartType = sweetheartType_NO;
                [HUD Prompt:@"网络错误"];
                //error
            }
        }
    }];
}
//删除所有的好友和恋人申请
- (void)deleteAllMySweetheartAndFriendApplyUser:(MLUser *)user TaUser:(MLUser *)taUSer
{
    //删除我们2个的恋人申请  //删除2人的好友申请
    AVQuery *q = [MLSweetheartApply query];
    [q whereKey:@"applier" equalTo:user];
    [q whereKey:@"user" equalTo:taUSer];
    AVQuery *q1 = [MLSweetheartApply query];
    [q1 whereKey:@"applier" equalTo:taUSer];
    [q1 whereKey:@"user" equalTo:user];
    
    AVQuery *q2 = [MLFriendApply query];
    [q2 whereKey:@"applier" equalTo:user];
    [q2 whereKey:@"user" equalTo:taUSer];
    AVQuery *q3 = [MLSweetheartApply query];
    [q3 whereKey:@"applier" equalTo:taUSer];
    [q3 whereKey:@"user" equalTo:user];
    
    
    AVQuery *dQ =[AVQuery orQueryWithSubqueries:[NSArray arrayWithObjects:q,q1,q2,q3, nil]];
    [dQ deleteAllInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        
    }];
    
    
    
}
//查询我和他以前的恋人关系,有则处理,没有就创建新的记录,并删除申请记录

- (void)acceptSweetheartApply:(UIButton *)sender {
    
    
    MLUser *user = [MLUser currentUser];
    if (user.type != MLUserType_NORMAL) {
        
        [HUD Prompt:@"你身份已改变,无法成为恋人"];
        
        return;
    }
    MLSweetheartApply *sweetheartApply = self.sweetheartApplyList[sender.tag];
    MLUser *applier = (MLUser *)sweetheartApply.applier;
    if(applier.type != MLUserType_NORMAL){
        [HUD Prompt:@"对方身份已改变,无法成为恋人"];
        return;
    }
    

    
    ShareData *shareData = [ShareData shareData];
    switch (shareData.sweetheartType) {
        case sweetheartType_NO:
            [self searchSweetheartCount:sender];
             //[HUD Prompt:@"正在查询当前恋人数"];
            return;
            break;
        case sweetheartType_QUERY:
            //[HUD Prompt:@"正在查询当前恋人数"];
            return;
            break;
        case sweetheartType_DONE:
            //查询完成,继续走下面方法
            break;
        default:
            break;
    }
    
    NSInteger number = 0;
    if ([ShareData shareData].firstView.sweetheart) {
        number++;
    }
    if ([ShareData shareData].secondView.sweetheart) {
        number++;
    }
    if ([ShareData shareData].thirdView.sweetheart) {
        number++;
    }
    if (number>=3) {
        [HUD Prompt:@"有3个未结束试恋，暂时不能申请"];
        return;
    }
   
     sender.enabled = NO;
    [AVAnalytics event:@"我愿意（试恋申请）"]; // 通知服务器一个点击事件。
   // MLSweetheartApply *sweetheartApply = self.sweetheartApplyList[sender.tag];
  //  MLUser *applier = (MLUser *)sweetheartApply.applier;
    //先查询对方的恋爱人数
    AVQuery *querySponorTa = [MLSweetheart query];
    [querySponorTa whereKey:@"sponsor" equalTo:applier];
    [querySponorTa whereKey:@"status" equalTo:[NSNumber numberWithInteger:1]];
    AVQuery *queryWinnerTa = [MLSweetheart query];
    [queryWinnerTa whereKey:@"winner" equalTo:applier];
    [queryWinnerTa whereKey:@"status" equalTo:[NSNumber numberWithInteger:1]];
    AVQuery *queryTa = [AVQuery orQueryWithSubqueries:[NSArray arrayWithObjects:queryWinnerTa,querySponorTa, nil]];
    [HUD setOnViewWithTitle:@"加载中" animated:YES];
    [queryTa findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [HUD hideFromViewAnimated:YES];
        BOOL is = NO;
        if (!error) {
            //没有错误
            if (objects) {
                if(objects.count<3){
                    is = YES;
                    //  [HUD Prompt:@"可以加为恋人"];
                }
            }
        }else{
            if ([[error localizedDescription] isEqualToString:@"no results matched the query"]) {
                is = YES;
                //  [HUD Prompt:@"可以加为恋人"];
                //没有匹配到结果
            }else{
                
                [HUD Prompt:@"网络错误"];
                //error
            }
        }
        //对方恋爱人数不够3人可以加为恋人
        if (is) {
            //先查询有没有记录
            shareData.sweetheartType = sweetheartType_QUERY;
            AVQuery *querySponor = [MLSweetheart query];
            [querySponor whereKey:@"sponsor" equalTo:sweetheartApply.user];
            [querySponor whereKey:@"winner" equalTo:sweetheartApply.applier];
            [querySponor includeKey:@"sponsor"];
            [querySponor includeKey:@"winner"];
            AVQuery *queryWinner = [MLSweetheart query];
            [queryWinner whereKey:@"winner" equalTo:sweetheartApply.user];
            [queryWinner whereKey:@"sponsor" equalTo:sweetheartApply.applier];
            [queryWinner includeKey:@"sponsor"];
            [queryWinner includeKey:@"winner"];
            AVQuery *query = [AVQuery orQueryWithSubqueries:[NSArray arrayWithObjects:queryWinner,querySponor, nil]];
            [HUD setOnViewWithTitle:@"加载中" animated:YES];
            [query includeKey:@"sponsor"];
            [query includeKey:@"winner"];
            [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
                [HUD hideFromViewAnimated:YES];
                if (!error) {
                    //没有错误
                    MLSweetheart *sweetheart = (MLSweetheart *)object;
                    //成功
                    [AVCloud callFunctionInBackground:@"getNetTime" withParameters:nil block:^(id objectt, NSError *errorr) {
                        //获取网络时间
                        if(errorr){
                            sender.enabled = YES;
                            [HUD Prompt:@"网络错误.返回重试"];
                        }else{
                            
                            NSString *str = objectt;
                            NSDate *date = [NSDate dateWithTimeIntervalSince1970:[str longLongValue]/1000];
                            sweetheart.togetherDate = date;
                            sweetheart.status = 1;
                            [sweetheart saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                                if (succeeded) {
                                
                                    
                                    [self deleteAllMySweetheartAndFriendApplyUser:(MLUser *)sweetheartApply.applier TaUser:(MLUser *)sweetheartApply.user];
                                    
//                                    sweetheartApply.status = MLSweetheartApplyStatus_SUCCEED;
//                                    [sweetheartApply saveInBackground];
                                    //保存恋人关系成功
                                    NSString *touserId = @"";
                                    if ([sweetheart.sponsor.objectId isEqualToString:[MLUser currentUser].objectId]) {
                                        touserId = sweetheart.winner.objectId;
                                    }else{
                                        touserId = sweetheart.sponsor.objectId;
                                    }
                                    // 发送透传,告诉对方成为恋人
                                    NSDictionary *params = @{@"touserId":  touserId, @"sweetId":sweetheart.objectId,@"fromuserId":[MLUser currentUser].objectId,@"nick":[MLUser currentUser].nickname,@"actions":ACTION_Pocketsweet_SWEET_ACCEPT};
                                    [AVCloud callFunctionInBackground:@"sendSweetApplyCMDMsg" withParameters:params block:^(id object, NSError *error) {
                                        if (!error) {
                                            [HUD Prompt:[NSString stringWithFormat:@"同意%@的试恋请求",applier.nickname]];
                                        } else {
                                            [HUD Prompt:@"网络错误,请重试"];
                                        }
                                    }];
                            
                                    [EaseSDKHelper sendTextMessage:@"我同意了您的试恋申请，未来48小时内我们以男女朋友对待。现在就开始吧。" to:applier.objectId messageType:eMessageTypeChat requireEncryption:NO messageExt:nil];
                                    //发送通知修改首页恋人关系
                                     [[NSNotificationCenter defaultCenter] postNotificationName:@"sweetheartChange" object:self userInfo:nil];
                                    //移除列表刷新
                                    [self.sweetheartApplyList removeObject:sweetheartApply];
                                    [self.tableView reloadData];
                                    sender.enabled = YES;
                                    if ([self.sweetheartApplyList count] == 0) {
                                        _uview.viewPrompt.hidden = NO;
                                    }else{
                                        _uview.viewPrompt.hidden = YES;
                                    }
                                    
                                }else{
                                    sender.enabled = YES;
                                    //失败
                                    [HUD Prompt:@"网络错误"];
                                }
                            }];
                        }
                    }];
                    
                }else{
                    if ([[error localizedDescription] isEqualToString:@"no results matched the query"]) {
                        //创建新的
                        //没有匹配到结果
                        [AVCloud callFunctionInBackground:@"getNetTime" withParameters:nil block:^(id objectt, NSError *errorr) {
                            //获取网络时间
                            if(errorr){
                                sender.enabled = YES;
                                [HUD Prompt:@"网络错误.返回重试"];
                            }else{
                                NSString *str = objectt;
                                NSDate *date = [NSDate dateWithTimeIntervalSince1970:[str longLongValue]/1000];
                                MLSweetheart *sweetheart = [MLSweetheart object];
                                sweetheart.sponsor = sweetheartApply.user;
                                sweetheart.winner = sweetheartApply.applier;
                                sweetheart.togetherDate = date;
                                sweetheart.status = 1;
                                [sweetheart saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                                    if (succeeded) {
//                                        sweetheartApply.status = MLSweetheartApplyStatus_SUCCEED;
//                                        [sweetheartApply saveInBackground];
                                         [self deleteAllMySweetheartAndFriendApplyUser:(MLUser *)sweetheartApply.applier TaUser:(MLUser *)sweetheartApply.user];
                                        //保存恋人关系成功
                                        // 发送透传,告诉对方成为恋人
                                        NSDictionary *params = @{@"touserId":  sweetheartApply.applier.objectId, @"sweetId":sweetheart.objectId, @"fromuserId":[MLUser currentUser].objectId,@"nick":[MLUser currentUser].nickname,@"actions":ACTION_Pocketsweet_SWEET_ACCEPT};
                                        [AVCloud callFunctionInBackground:@"sendSweetApplyCMDMsg" withParameters:params block:^(id object, NSError *error) {
                                            if (!error) {
                                                [HUD Prompt:[NSString stringWithFormat:@"同意%@的试恋请求",applier.nickname]];
                                            } else {
                                                [HUD Prompt:@"网络错误,请重试"];
                                            }
                                        }];
                                         [[NSNotificationCenter defaultCenter] postNotificationName:@"sweetheartChange" object:self userInfo:nil];
                                        //移除列表刷新
                                        [self.sweetheartApplyList removeObject:sweetheartApply];
                                        [self.tableView reloadData];
                                        sender.enabled = YES;
                                        if ([self.sweetheartApplyList count] == 0) {
                                            _uview.viewPrompt.hidden = NO;
                                        }else{
                                            _uview.viewPrompt.hidden = YES;
                                        }
                                    }else{
                                        sender.enabled = YES;
                                        //失败
                                        [HUD Prompt:@"网络错误"];
                                    }
                                }];
                            }
                            
                        }];
                    }else{
                        
                        sender.enabled = YES;
                        [HUD Prompt:@"网络错误"];
                        //error
                        // [HUD Prompt:[error localizedDescription]];
                    }
                    
                }
                
                
            }];
            //先查询记录是否存在,存在处理,不存在跳过
            //可以加为恋人
            //先判断是否是好友,不是好友则加为好友
            if([Utils isFriendObjectId:applier.objectId]){
                //已经是好友了,不需要再加为好友了
                
            }else{
                //还不是好友,先加为好友
                [[MLUser currentUser] follow:applier.objectId andCallback:^(BOOL succeeded, NSError *error) {
                    if (succeeded || error.code == kAVErrorDuplicateValue) {
                        [self addShareDateFriendsUser:applier];
                        NSDictionary *params = @{@"touserId": applier.objectId, @"fromuserId": [MLUser currentUser].objectId,@"nick":[MLUser currentUser].nickname,@"actions":ACTION_FRIEND_APPLY_ACCEPTED};
                        [AVCloud callFunctionInBackground:@"sendFriendApplyMsg" withParameters:params block:^(id object, NSError *error) {
                            if (object) {
                                // NSLog(@"%@",object);
                            }
                        }];
                    }
                }];
            }
        }
        
    }];
    
   
}

- (void)rejectAllApply {
    // 忽略所有
    if (![self.sweetheartApplyList count]) {
        [HUD Prompt:@"没有可以忽略的~"];
        return;
    }
    HUD*hud = [[HUD alloc]init];
    [hud HUDImage:nil Title:@"忽略所有" Content:@"确定要忽略所有试恋申请?" CANCEL:@"取消" Confirm:@"确定"];
    hud.Confirm  = ^{
        for (MLSweetheartApply *sweetheartApply in self.sweetheartApplyList) {
            //[sweetheartApply deleteInBackground];
            sweetheartApply.status = MLSweetheartApplyStatus_FAILED;
            [sweetheartApply saveInBackground];
        }
        [self.sweetheartApplyList removeAllObjects];
        if (self.sweetheartApplyList.count==0) {
            _uview.viewPrompt.hidden = NO;
        }
        
        // 刷新列表
        [self.tableView reloadData];
    };
    hud.Cancel = ^{
        NSLog(@"取消");
    };
    [self.view addSubview:hud];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
